Welcome back. Last time you saw that on the right side of an assignment statement,
multiple expressions separated by commas will be automatically packed into a tuple.
So, line one the variable Julia gets bound to a tuple,
square bracket zero will be Julia,
square bracket one will be Roberts and so on.
But, instead of referring to the elements of
that tuple with square brackets zero and square bracket as one,
we can unpack them all at once into a bunch of different variable names.
So, we've put a bunch of variable names here on
the left side and what that does it's as if we've said,
name equals Julia square bracket zero,
surname equals Julia square brackets
one, and so on.
So, it's just going to take Julia,
which is a tuple and take all of the values from that and put the first value,
it does goes positionally just like we do when we're calling functions,
the first value goes to the first variable name,
the second value goes to the second variable name, and so on.
Notice however that the number of variable names on the left-hand side of
the assignment statement has
the equal the number of values that are on the right hand side.
If I run a, b, c, d,
equals one, two, three,
I will get an error.
We need more than three values to unpack on line one because we have four variable names.
So, if I have four variable names,
that'll be better, it works.
But, if I have five variable names,
I also get an error.
Too many values, not enough variable names.
There's even a way to pass a tuple to a function and have
them automatically unpacked into the parameter names.
In this code, we've defined a function add that just returns the sum of its two inputs.
If I say to add three and four,
that will give me seven.
Now, I can take five and four and I can tell
the add function to get your values from the tuple z.
On line six I'm saying to add the contents of
the tuples z and it unpacks the first element of z into the variable x.
So, x gets five,
y gets four, and we see that nine prints out.
Now, I had to do a special little notation,
this asterisk in order to tell the Python interpreter that I wanted z to
be treated as a tuple whose components would be
unpacked and assigned to the two parameter names x and y.
If instead I tried to just say add of z,
I'm going to get an error
because what it's going to try to do is treat z as a single value,
assign that value to x,
and then it looks for what value should I give to y and there's nothing for y.
So, I get an error here.
Add takes exactly two arguments.
It has two parameter names x and y.
That's what the error message is telling us,
but only one was given.
We only gave it a single value,
the tuple five, four that didn't work.
There was no problem on line six though because the star z said, "Hey,
even though it's a single value,
a tuple five, four,
you should unpack that value and five should go for
the first variable name and four should go for the second one."
Unpacking is particularly useful for making iteration code more readable.
For example, when you iterate over a list of tuples.
In this code, we iterate over the key value pairs in a dictionary.
So, we ask for all of the items from D. So,
that's going to give us a tuple with k1 and three,
and then another tuple with k2 and seven, and so on.
We get a sequence of all of these and we're iterating through that sequence.
So, in line four the value of p will be one of these tuples.
We can ask for p square bracket zero to get k1 and
p square bracket one to get three.
Let's see what happens when that runs.
We have this key k1 value three that's coming from the first tuple.
Remember, we have this format string where
we're substituting in p square bracket zero there,
so we get key colon and then we're substituting in k1,
and then just the word value colon and we substitute in
whatever p square bracket one is at this point in the string,
so we get the three.
On the next iteration,
p is bound to k2 and seven and so we get key colon k2 value colon seven.
Reading that code is a little hard because you have to
remember that when you're using this p square bracket zero,
that's a p was a tuple,
that tuple was representing a key value pair in the dictionary.
Therefore, a p square bracket zero must be the key.
We can use mnemonic variable names to help us make that a little bit more readable.
Let's do this in two steps.
Suppose I say k equals p square bracket zero and v equals p square bracket one,
then I can k and v
here and this is just if I pick a good variable name
like k for representing a key and v for representing a value,
it's easier to read lines six and remember that what I'm
printing out is the key and the value.
Once we have that idea,
we can go even further and instead of unpacking ourselves on line four and five,
we can have Python do it for us.
So, we can stick to variable names here k comma v,
and it's going to automatically take each item and unpack it into
the two variables k and v. This is going to give me exactly the same result.
So, that's unpacking.
You've got one tuple and multiple variable names.
The tuple has to have the same number of values as the number of variable names.
We can do that with explicit assignment where there's
an equal sign or we can do it when there's behind the scenes assignment,
like iterator variables in a for loop as you've seen here,
k and v are having behind the scenes assignment to
a tuple and we're getting them to get unpacked into the two variable names
k and v. There's also behind the scenes assignment whenever you have
parameter names in a function and we can also do the automatic unpacking there,
but that requires you to use the star notation to tell
Python to do the unpacking. I'll see you next time.